﻿/* 
 * PerformanceControl.cs
 * 
 * Author - Ankit Gupta and Gaurav Aggarwal
 * 
 * This file acts as a controller to Performance tab.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security;
using System.Diagnostics;

partial class PerformanceControl : UserControl
{
    // Apply the changes
    private void applyButton_Click(object sender, EventArgs e)
    {
        try
        {
            // Validate whether entered data is in proper format or not
            if (validatedata() == false)
                return;

            // Cache Size
            if ("98304".Equals(cacheSizeTextBox.Text))
            {
                serviceParameters.DeleteValue("CacheSize", false);
            }
            else
            {
                serviceParameters.SetValue("CacheSize", Int32.Parse(cacheSizeTextBox.Text));
            }

            // Non Persistent Caching
            if ("%TEMP%AFSCache".Equals(cacheLocationTextBox.Text))
            {
                serviceParameters.DeleteValue("CachePath", false);
            }
            else
            {
                serviceParameters.SetValue("CachePath", cacheLocationTextBox.Text, RegistryValueKind.ExpandString);
            }

            if (nonPersistentCachingCheckBox.Checked)
            {
                serviceParameters.SetValue("NonPersistentCaching", 1, RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.DeleteValue("NonPersistentCaching", false);
            }

            // Validate Cache
            if ("0".Equals(validateCacheComboBox.SelectedIndex))
            {
                serviceParameters.DeleteValue("ValidateCache", false);
            }
            else
            {
                serviceParameters.SetValue("ValidateCache", validateCacheComboBox.SelectedIndex);
            }

            // Num of Stat Cache Entries
            if ("10000".Equals(numCacheEntriesTextBox.Text))
            {
                serviceParameters.DeleteValue("Stats", false);
            }
            else
            {
                serviceParameters.SetValue("Stats", Int32.Parse(numCacheEntriesTextBox.Text));
            }

            // Connection Dead Timeout
            if ("60".Equals(connDeadTimeoutTextBox.Text))
            {
                serviceParameters.DeleteValue("ConnDeadTimeout", false);
            }
            else
            {
                serviceParameters.SetValue("ConnDeadTimeout", Int32.Parse(connDeadTimeoutTextBox.Text));
            }

            // Chunk Size
            if ("20".Equals(chunkSizeTextBox.Text))
            {
                serviceParameters.DeleteValue("ChunkSize", false);
            }
            else
            {
                serviceParameters.SetValue("ChunkSize", Int32.Parse(chunkSizeTextBox.Text));
            }

            // Hard Dead Timeout
            if ("120".Equals(hardDeadTimeoutTextBox.Text))
            {
                serviceParameters.DeleteValue("HardDeadTimeout", false);
            }
            else
            {
                serviceParameters.SetValue("HardDeadTimeout", Int32.Parse(hardDeadTimeoutTextBox.Text));
            }

            // Max num of CPUs
            if ("0".Equals(numCPUTextBox.Text))
            {
                serviceParameters.DeleteValue("MaxCPUs", false);
            }
            else
            {
                if(numCPUTextBox.Text.Equals("(Unlimited)"))
                    serviceParameters.SetValue("MaxCPUs", 0);     
                else
                    serviceParameters.SetValue("MaxCPUs", Int32.Parse(numCPUTextBox.Text));
            }

            // Down Servers
            if ("180".Equals(downServersTextBox.Text))
            {
                serviceParameters.DeleteValue("daemonCheckDownInterval", false);
            }
            else
            {
                serviceParameters.SetValue("daemonCheckDownInterval", Int32.Parse(downServersTextBox.Text));
            }

            // Background Daemons
            if ("2".Equals(numDaemonsTextBox.Text))
            {
                serviceParameters.DeleteValue("Daemons", false);
            }
            else
            {
                serviceParameters.SetValue("Daemons", Int32.Parse(numDaemonsTextBox.Text));
            }

            // Up Servers
            if ("600".Equals(upServersTextBox.Text))
            {
                serviceParameters.DeleteValue("daemonCheckUpInterval", false);
            }
            else
            {
                serviceParameters.SetValue("daemonCheckUpInterval", Int32.Parse(upServersTextBox.Text));
            }

            // Server Threads
            if ("25".Equals(numServerThreadsTextBox.Text))
            {
                serviceParameters.DeleteValue("ServerThreads", false);
            }
            else
            {
                serviceParameters.SetValue("ServerThreads", Int32.Parse(numServerThreadsTextBox.Text));
            }

            // Callback Invalidation
            if ("60".Equals(callbackInvalidationTextBox.Text))
            {
                serviceParameters.DeleteValue("daemonCheckCBInterval", false);
            }
            else
            {
                serviceParameters.SetValue("daemonCheckCBInterval", Int32.Parse(callbackInvalidationTextBox.Text));
            }

            // Max MTU
            if ("0".Equals(MTURxInterfaceTextBox.Text))
            {
                serviceParameters.DeleteValue("RxMaxMTU", false);
            }
            else
            {
                if(MTURxInterfaceTextBox.Text.Equals("(Automatic)"))
                    serviceParameters.SetValue("RxMaxMTU", 0);
                else
                    serviceParameters.SetValue("RxMaxMTU", Int32.Parse(MTURxInterfaceTextBox.Text));
            }

            // File Locks
            if ("60".Equals(fileLocksTextBox.Text))
            {
                serviceParameters.DeleteValue("daemonCheckLockInterval", false);
            }
            else
            {
                serviceParameters.SetValue("daemonCheckLockInterval", Int32.Parse(fileLocksTextBox.Text));
            }

            // Token Expiration
            if ("180".Equals(tokenExpirationTextBox.Text))
            {
                serviceParameters.DeleteValue("daemonCheckTokenInterval", false);
            }
            else
            {
                serviceParameters.SetValue("daemonCheckTokenInterval", Int32.Parse(tokenExpirationTextBox.Text));
            }

            // Disable RX Jumbograms
            if (disableRxJumbogramCheckBox.Checked)
            {
                serviceParameters.SetValue("RxNoJumbo", 1, RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.DeleteValue("RxNoJumbo", false);
            }
        }
        catch (SecurityException)
        {
            MessageBox.Show("You do not have access to apply these settings.", "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    // Cancel all the operations
    private void cancelButton_Click(object sender, EventArgs e)
    {
        Process[] p = Process.GetProcessesByName("mmc");
        p[0].Kill();
    }
    
    // Error checking
    private bool validatedata()
    {
        if (!checkint(cacheSizeTextBox.Text))
        {
            String error = "Cache Size should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if (validateCacheComboBox.SelectedIndex == -1)
        {
            String error = "Validate Cache should be one of the given values";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(numCacheEntriesTextBox.Text))
        {
            String error = "Number of Static Cache Entries should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(connDeadTimeoutTextBox.Text))
        {
            String error = "Connection Dead Timeout should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(chunkSizeTextBox.Text))
        {
            String error = "Chunk Size should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(hardDeadTimeoutTextBox.Text))
        {
            String error = "Hard Dead Timeout should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(numCPUTextBox.Text)&& !numCPUTextBox.Text.Equals("(Unlimited)"))
        {
            String error = "Number of CPU should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(downServersTextBox.Text))
        {
            String error = "Probe interval for Down Servers should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(numDaemonsTextBox.Text))
        {
            String error = "Number of Background Daemons should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(upServersTextBox.Text))
        {
            String error = "Probe interval for Up Servers should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(numServerThreadsTextBox.Text))
        {
            String error = "Number of Server Threads should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(callbackInvalidationTextBox.Text))
        {
            String error = "Probe interval for CallBack Invalidation should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(MTURxInterfaceTextBox.Text)&& !MTURxInterfaceTextBox.Text.Equals("(Automatic)"))
        {
            String error = "MTU should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(fileLocksTextBox.Text))
        {
            String error = "Probe interval for File Locks should be an integer";
            MessageBox.Show(error);
            return false;
        }
        if(!checkint(tokenExpirationTextBox.Text))
        {
            String error = "Probe interval for Token Expiration should be an integer";
            MessageBox.Show(error);
            return false;
        }
        return true;
    }

    private bool checkint(string a)
    {
        bool flag = true;
        
        if(a.Equals("")) flag = false;

        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] < 48 || a[i] > 57) flag = false;
        }
        return flag;
    }
}